/**
 * Title: BaseController.java
 * @author WuJin
 * @date 2020年3月31日
 * @version 1.0
 */
package com.pm.controller;

import java.util.List;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.pm.common.Pages;
import com.pm.common.Response;
import com.pm.common.exception.ExpCodeState;
import com.pm.service.BaseService;

import io.swagger.annotations.ApiOperation;

/**
 * Title: BaseController
 * 
 * @author WuJin
 * @date 2020年3月31日
 */
public abstract class BaseController<T> {
	private Response<?> response = new Response<>();

	/**
	 * 实现这个抽象方法 将继承者业务逻辑对象返回
	 * Title: getBaseService
	 * 
	 * @return
	 */
	protected abstract BaseService<T> getBaseService();

	/**
	 * 新增 Title: add
	 * 
	 * @param t
	 * @return
	 */
	@ApiOperation("通用-新增接口")
	@PostMapping("/add")
	@ResponseBody
	public Response<?> add(T t) {
		Integer result = getBaseService().add(t);
		if (result > 0) {
			response = new Response<>(ExpCodeState.add_data_success, result);
		} else {
			response = new Response<>(ExpCodeState.add_data_failed);
		}
		return response;
	}

	@ApiOperation("通用-更新接口")
	@PostMapping("/update")
	@ResponseBody
	public Response<?> update(T t) {
		Integer result = getBaseService().update(t);
		if (result > 0) {
			response = new Response<>(ExpCodeState.update_data_success, result);
		} else {
			response = new Response<>(ExpCodeState.update_data_failed);
		}
		return response;
	}

	@ApiOperation("通用-删除接口")
	@PostMapping("/remove")
	@ResponseBody
	public Response<?> remove(T t) {
		Integer result = getBaseService().remove(t);
		if (result > 0) {
			response = new Response<>(ExpCodeState.remove_data_success, result);
		} else {
			response = new Response<>(ExpCodeState.remove_data_failed);
		}
		return response;
	}

	@ApiOperation("通用-单个对象查询接口")
	@PostMapping("/load")
	@ResponseBody
	public Response<?> load(T t) {
		T result = getBaseService().load(t);
		response = new Response<T>(ExpCodeState.query_data_success, result);
		return response;
	}

	@ApiOperation("通用-全部列表查询接口")
	@PostMapping("/getList")
	@ResponseBody
	public Response<List<T>> getList(T t) {
		List<T> result = getBaseService().getList(t);
		Response<List<T>> res = new Response<List<T>>(ExpCodeState.query_data_success, result);
		return res;
	}

	@ApiOperation("通用-分页查询接口")
	@PostMapping("/getListByPage/{pageNum}/{pageSize}")
	@ResponseBody
	public Response<PageInfo<T>> getList(T t, Pages page) {
		PageInfo<T> result = getBaseService().getListByPage(t, page);
		Response<PageInfo<T>> res = new Response<PageInfo<T>>(ExpCodeState.query_data_success, result);
		return res;
	}
}
